package cn.baage.campus.config.security.service.impl;

import cn.baage.campus.config.security.BaseUserDetail;
import cn.baage.campus.config.security.service.BaseUserDetailsService;
import cn.baage.campus.model.dto.LoginUser;
import cn.baage.campus.model.po.Role;
import cn.baage.campus.model.dto.UserRoleDTO;
import cn.baage.campus.service.UserService;
import cn.baage.campus.utils.SecurityUtils;
import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


/**
 * @Description: 用户名 UserDetailsService 实现
 * @Author: 八阿哥
 * @url: baage.cn
 * @Version: 1.0
 **/
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class BaseUserDetailsServiceImpl implements BaseUserDetailsService {

    private final UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserRoleDTO userRoleDTO = Optional.ofNullable(userService.getAuthInfoByUserName(username))
                .orElseGet(UserRoleDTO::new);
        if (userRoleDTO.getPassword().isEmpty()) {
            throw new UsernameNotFoundException("用户不存在！");
        }
        return SecurityUtils.user2UserDetail(userRoleDTO);
    }


}